package com.upc.tp.timeplanning.controller;

import com.upc.tp.timeplanning.entity.*;
import com.upc.tp.timeplanning.mapper.RuleMapper;
import com.upc.tp.timeplanning.mapper.TaskMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/task", method = {RequestMethod.POST, RequestMethod.GET})
public class TaskController {

    @Autowired
    TaskMapper taskMapper;
    @Autowired
    RuleMapper ruleMapper;

    @RequestMapping("/getAll")
    public List<Task> getAll(){
        List<Task> list = taskMapper.getAll();
        return list;
    }

    @RequestMapping("/getByUsername")  //查询该学生的全部计划
    public ResBody getByUsername(@RequestBody Student task){

        ResBody resBody;
        try{
            List<Task> list = taskMapper.getByUsername(task.getUsername());
            resBody = new ResBody(100,"success",list);
        }catch (Exception e ){
            return new ResBody(0,"fail",null);
        }

        return resBody;
    }

    @RequestMapping("/getByDate")  //查询该学生的全部计划
    public ResBody getByDate(@RequestBody Task task){

        ResBody resBody;
        try{
            List<Task> list = taskMapper.getByDate(task.getUsername(),task.getStart_day(),task.getEnd_day());
            resBody = new ResBody(100,"success",list);
        }catch (Exception e ){
            return new ResBody(0,"fail",null);
        }

        return resBody;
    }

    @RequestMapping("/insert")
    public ResBody insert(@RequestBody Task task) {

        ResBody res;
        try {
            //taskMapper.insert(task.getUsername(), task.getContent(),task.getCreation_time(),task.getSubject(),task.getType(),task.getStart_time(),task.getEnd_time(),task.getRule_id(),task.getParent_pass(),task.getFinish());
            Rule rule = ruleMapper.getCycle(task.getRule_id());
            int cycle = rule.getCycle();
            Integer repeat_time;
            if(cycle == 0){
                repeat_time = 1;  //如果rule_id=1，即不重复，重复次数repeat_time设为1
            }
            else{
                long days = (task.getEnd_day().getTime() - task.getStart_day().getTime())/(24*3600*1000);
                repeat_time = ((int)days/cycle)+1 ;
            }
            taskMapper.insert(task.getUsername(), task.getContent(),task.getCreation_time(),task.getSubject(),task.getType(),task.getStart_time(),task.getEnd_time(),task.getStart_day(),task.getEnd_day(),task.getRule_id(),task.getParent_pass(),task.getFinish(),repeat_time);
            res = new ResBody(100, "success", null);
        } catch (Exception e) {
            return new ResBody(0, "fail", null);
        }
        return res;
    }

//    @RequestMapping("/updateRepeatTime")
//    public ResBody updateRepeatTime(@RequestBody Task task){
//        ResBody res;
//
//        try {
//            Rule rule = ruleMapper.getCycle(task.getRule_id());
//            int cycle = rule.getCycle();
//            long days = (task.getEnd_time().getTime() - task.getStart_time().getTime())/(24*3600*1000);
//            int repeat_time = (int)days/cycle ;
//            taskMapper.updateRepeatTime(repeat_time,task.getTask_id());
//            res = new ResBody(100, "success", null);
//        } catch (Exception e) {
//            return new ResBody(0, "fail", null);
//        }
//        return res;
//    }

    @RequestMapping("/checkByUsername")  //查询该学生未经家长同意的计划
    public ResBody checkByUsername(@RequestBody Student student){

        ResBody resBody;
        try{
            List<Task> list = taskMapper.checkByUsername(student.getUsername());
            resBody = new ResBody(100,"success",list);
        }catch (Exception e ){
            return new ResBody(0,"fail",null);
        }
        return resBody;
    }

    @RequestMapping("/passSingle")   //通过单条计划
    public ResBody passSingle(@RequestBody Task task){
        ResBody res;

        try {
            taskMapper.passSingle(task.getTask_id());
            res = new ResBody(100, "success", null);
        } catch (Exception e) {
            return new ResBody(0, "fail", null);
        }
        return res;
    }

    @RequestMapping("/passAll")  //一键通过该学生全部计划
    public ResBody passAll(@RequestBody Task task){
        ResBody res;

        try {
            taskMapper.passAll(task.getUsername());
            res = new ResBody(100, "success", null);
        } catch (Exception e) {
            return new ResBody(0, "fail", null);
        }
        return res;
    }

    @RequestMapping("/delete")
    public ResBody delete(@RequestBody Task task){

        ResBody resBody;
        try{
            taskMapper.delete(task.getRule_id());
            resBody = new ResBody(100,"success",null);
        }catch (Exception e ){
            return new ResBody(0,"fail",null);
        }

        return resBody;
    }
}
